﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "SpriteFrame.h"
#include "../base/Debuger.h"
#include "../base/GameWorld.h"
#include "../render/TextureCache.h"
#include "../render/Texture2D.h"
#include "../base/Size.h"

namespace SkyEngine2d
{
	
	std::shared_ptr<SpriteFrame> SpriteFrame::createFromPicture(const std::wstring & fileName, const Rect & des_rect_in_tex)
	{
		auto sf = std::shared_ptr< SpriteFrame>(new SpriteFrame);
		if (sf->initWithTextureFilename(fileName, des_rect_in_tex))
		{
			return sf;
		}
		return std::shared_ptr<SpriteFrame>();
	}
	
	
	std::shared_ptr<SpriteFrame> SpriteFrame::createFromTexture(const std::shared_ptr<Texture2D>& testure, const Rect & des_rect_in_tex)
	{
		auto p = std::shared_ptr<SpriteFrame>(new SpriteFrame);
		if (p->initWithTexture(testure, des_rect_in_tex, false, Vector2(), Size()))
		{
			return p;
		}
		return std::shared_ptr<SpriteFrame>();
	}
	
	
	const Rect & SpriteFrame::getRect() const
	{
		return m_rect;
	}
	
	
	void SpriteFrame::setRect(const Rect & rect)
	{
		m_rect = rect;
	}
	
	
	std::shared_ptr<SpriteFrame> SpriteFrame::createFromPicture(const std::wstring &file_name)
	{
		auto p = std::shared_ptr<SpriteFrame>(new SpriteFrame);
		if (p&&p->initWithTextureFileName(file_name))
		{
			return p;
		}
		return nullptr;
	}
	
	std::weak_ptr<Texture2D> SpriteFrame::getTexture()
	{
		return m_texture;
	}
	
	const Size& SpriteFrame::getContentSize() const
	{
		return m_rect.getSize();
	}

	SpriteFrame::SpriteFrame()
		:m_rect(0,0,0,0)
		,m_texture(nullptr)
	{
	
	}
	
	
	bool SpriteFrame::initWithTextureFileName(const std::wstring & file_name)
	{
		m_textureFileName = file_name;
		if (m_textureFileName.empty())
		{
			DebugWarning("纹理图片名字为空");
			return false;
		}
		m_texture = GameWorld::getInstance()->getTextureCache()->addImage(m_textureFileName);
		if (!m_texture)
		{
			DebugWarning("纹理初始化失败");
			return false;
		}
		m_rect.setSize(m_texture->getContentSize());
		
		return true;
	}
	
	SpriteFrame::~SpriteFrame()
	{
	
	}
	
	bool SpriteFrame::initWithTextureFilename(const std::wstring & fileName, const Rect & rect, bool rotated, const Vector2 offset, const Size &originalSize)
	{
		m_rect = rect;
		m_texture = NULL;
		m_textureFileName = fileName;
		return true;
	}
	
	bool SpriteFrame::initWithTextureFilename(const std::wstring & file_name, const Rect & rect)
	{
		m_textureFileName = file_name;
		if (m_textureFileName.empty())
		{
			DebugWarning("纹理图片名字为空");
			return false;
		}
		m_texture = GameWorld::getInstance()->getTextureCache()->addImage(m_textureFileName);
		if (!m_texture)
		{
			DebugWarning("纹理初始化失败");
			return false;
		}
		m_rect = rect;
		return true;
	}
	
	bool SpriteFrame::initWithTexture(const std::shared_ptr<Texture2D> & texture, const Rect& rect, bool rotated, const Vector2 &offset, const Size & originalSize)
	{
		m_texture = texture;
		m_rect = rect;
		return true;
	}
}

